package com.nulldev.util.web.nCaptcha.captchas.obftext;

import java.awt.image.BufferedImage;

import com.nulldev.util.image.ImageUtil;
import com.nulldev.util.image2.textobf.TextObf;
import com.nulldev.util.image2.textobf.TextObf.ObfuscationMethod;
import com.nulldev.util.java.DataSet;
import com.nulldev.util.java.swing.font.FontManager;
import com.nulldev.util.math.MathUtil;
import com.nulldev.util.string.StringsUtil;
import com.nulldev.util.web.nCaptcha.backend.Captcha;

public class ImageTextCaptcha implements Captcha {

	@Override
	public DataSet<String, String[]> generateBase64Image(final int botLevel) {
		final String word = StringsUtil.randomString(6);
		final BufferedImage obf = TextObf.obfuscate(word
				, (botLevel > 1 ? ObfuscationMethod.Noise : ObfuscationMethod.PixelObfuscation), 
				FontManager.getFontSet(MathUtil.randInt(14, 16)));
		final String base64 = ImageUtil.encodeToString(obf, "png");
		return new DataSet<String, String[]>("data:image/png;base64," + base64, new String[] {word});
	}

	@Override
	public String getType() {
		return "screenwords";
	}
}
